home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / recover2.c < prev    next >
C/C++ Source or Header  |  1994-11-15  |  8KB  |  320 lines

  1. /*
  2.  *                Recover2.c
  3.  *
  4.  * Rebuilds trashed CTDLROOM.SYS.
  5.  */
  6.  
  7. #include "ctdl.h"    /* header file  */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <fctype.h>
  12. #include <math.h>
  13. #include <ctype.h>
  14. #include <time.h>
  15. #include <proto/exec.h>
  16. #include <dos/dos.h>
  17. #include <pragmas/dos_pragmas.h>
  18. #include "exec/memory.h"
  19. #include "exec/ports.h"
  20. #include "exec/exec.h"
  21.  
  22.  
  23. /*
  24.  *                History
  25.  *
  26.  * 87Oct11 HAW  V3 update.
  27.  * 85Nov16 HAW  Modified for MS-DOS libraries.
  28.  * 85Apr15 HAW  Move to MS-DOS.
  29.  * 84Aug16 HAW  Now no need to reconfigure afterwards, gives stats.
  30.  * 84Aug15 HAW  Documented.
  31.  * 84Aug15 HAW  Created (In the wee hours).
  32.  */
  33.  
  34. /*
  35.  *                Contents
  36.  *
  37.  *    crashout()        irrecoverable error
  38.  *    doRest()        Read msgs.
  39.  *    handle()        Inserts msg into room slot, etc.
  40.  *    indexRooms()        build RAM index to ctdlroom.sys
  41.  *    init()            Open up files for recovery effort.
  42.  *    main()            Master control.
  43.  *    noteRoom()        enter room into RAM index
  44.  *    XzapRoomFile()        Zaps room file for you.
  45.  */
  46.  
  47. extern CONFIG    cfg;    /* The main variable to be saved    */
  48. extern MessageBuffer msgBuf;    /* The -sole- message buffer    */
  49. extern FILE    *msgfl, *msgfl2; /* file descriptor for the msg file    */
  50. extern FILE        *upfd;
  51. extern rTable    *roomTab;    /* RAM index of rooms    */
  52. extern aRoom    roomBuf;    /* room buffer    */
  53. extern FILE    *roomfl;    /* file descriptor for rooms    */
  54. extern int    thisRoom;    /* room currently in roomBuf    */
  55.  
  56. FILE *fopen();
  57. char handle(MSG_NUMBER mess);
  58. void init(void);
  59. void XzapRoomFile(void);
  60. void doRest(void);
  61. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  62.  
  63. /*
  64.  * crashout()
  65.  *
  66.  * This handles the irrecoverable error.
  67.  */
  68. void crashout(str)
  69. char *str;
  70. {
  71.     exit(printf(str));
  72. }
  73.  
  74. /*
  75.  * doRest()
  76.  *
  77.  * This function loops thru the msg file until finished, reconstructing the
  78.  * rooms and their contents from the messages found.
  79.  */
  80. void doRest()
  81. {
  82.     MSG_NUMBER message, firstMessage;
  83.     int total, mailCount;    /* For stat keeping. */
  84.     extern struct mBuf mFile1;
  85.  
  86.     startAt(msgfl, &mFile1, 0, 0);
  87.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  88.     message = atol(msgBuf.mbId);
  89.     printf("%ld\n", message);
  90.     firstMessage = message;
  91.     mailCount = 0;
  92.     if (handle(message)) mailCount++;
  93.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  94.     message = atol(msgBuf.mbId);
  95.     total = 1;
  96.     while (message != firstMessage) {
  97.     total++;
  98.     printf("%ld\n", message);
  99.     if (handle(message)) mailCount++;
  100.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  101.     message = atol(msgBuf.mbId);
  102.     }
  103.     printf("We have %u messages, with %u of them being in the mail.\n",
  104.                         total, mailCount);
  105. }
  106.  
  107. /*
  108.  * handle()
  109.  *
  110.  * This function handles setting up and saving room indexes, etc, based on the
  111.  * current message.
  112.  */
  113. char handle(mess)
  114. MSG_NUMBER mess;
  115. {
  116.     int  i, j, k;
  117.  
  118.     if (msgBuf.mbroom[0] == '\0') return FALSE; /* Can't handle */
  119.     printf("Message for %s\n", msgBuf.mbroom);
  120.     for (i = 0; i < MAXROOMS; i++)
  121.     if (roomTab[i].rtname[0] == 0) break;
  122.     else if (strCmpU(roomTab[i].rtname, msgBuf.mbroom) == 0) break;
  123.  
  124.     if (i == MAXROOMS) return FALSE;    /* If no room left in room    */
  125.     if (i == MAILROOM) return TRUE;    /* file or room is Mail don't do*/
  126.                     /* anything.            */
  127.     printf("Being put in slot %d\n", i);
  128.     getRoom(i);                /* Get appropriate slot    */
  129.     strcpy(roomTab[i].rtname, msgBuf.mbroom);   /* Copy whether needs it*/
  130.     strcpy(roomBuf.rbname, msgBuf.mbroom);
  131.     if (roomBuf.rbflags.INUSE == FALSE)
  132.     for (j = 0; j < MSGSPERRM - 1; j++)
  133.         roomBuf.msg[j].rbmsgNo = 0l;
  134.     roomBuf.rbflags.INUSE = TRUE;
  135.     for (k = 0; roomBuf.msg[k+1].rbmsgNo < mess && k < MSGSPERRM-1; k++)
  136.     ;
  137.     for (j = 0; j < k; j++) {    /* Move msgs up 1.    */
  138.     roomBuf.msg[j].rbmsgLoc = roomBuf.msg[j+1].rbmsgLoc;
  139.     roomBuf.msg[j].rbmsgNo  = roomBuf.msg[j+1].rbmsgNo;
  140.     }
  141.     roomBuf.msg[k].rbmsgNo  = mess;/* And now insert msg    */
  142.     roomBuf.msg[k].rbmsgLoc = msgBuf.mbheadSector;
  143.     putRoom(i);    /* And save our folly. */
  144.     return FALSE;
  145. }
  146.  
  147. /*
  148.  * indexRooms()
  149.  *
  150.  * This will build a RAM index to CTDLROOM.SYS, and delete empty rooms.
  151.  */
  152. void indexRooms()
  153. {
  154.     int  goodRoom, m, roomCount, slot;
  155.  
  156.     roomCount = 0;
  157.     for (slot = 0;  slot < MAXROOMS;  slot++) {
  158.     getRoom(slot);
  159.     if (roomBuf.rbflags.INUSE == 1) {
  160.         roomBuf.rbflags.INUSE = 0;        /* clear "inUse" flag */
  161.         for (m = 0, goodRoom = FALSE; m < MSGSPERRM && !goodRoom; m++) {
  162.         if (roomBuf.msg[m].rbmsgNo > cfg.oldest) {
  163.             goodRoom    = TRUE;
  164.         }
  165.         }
  166.         if (goodRoom   || roomBuf.rbflags.PERMROOM == 1)   {
  167.         roomBuf.rbflags.INUSE = 1;
  168.         }
  169.  
  170.         if (roomBuf.rbflags.INUSE == 1)
  171.         roomCount++;
  172.         else {
  173.         roomBuf.rbflags.INUSE    = 0;
  174.         roomBuf.rbflags.ISDIR   = 0;
  175.         roomBuf.rbflags.PERMROOM = 0;
  176.         roomBuf.rbflags.INUSE    = 0;
  177.         putRoom(slot);
  178.         }
  179.     }
  180.     noteRoom();
  181.     }
  182. }
  183.  
  184. /*
  185.  * init()
  186.  *
  187.  * This function will set up the files for recovery.
  188.  */
  189. void init()
  190. {
  191.     char c;
  192.     SYS_FILE temp;
  193.     extern char *R_W_ANY, *W_R_ANY;
  194.  
  195.     cfg.weAre = UTILITY;
  196.     if (!readSysTab(TRUE, TRUE)) crashout("\nDisaster!  I need CTDLTABL.SYS!");
  197.     cfg.weAre = UTILITY;
  198.     mvToHomeDisk(&cfg.homeArea);
  199.  
  200.     if (access(LOCKFILE, 0) != ERROR) {
  201.     printf("Please do not run Recover2 using Outside Commands.\n");
  202.     writeSysTab();
  203.     exit(1);
  204.     }
  205.     /* open message file */
  206.  
  207.     InitMsgBase();
  208.  
  209.     makeSysName(temp, "ctdlroom.sys", &cfg.roomArea);
  210.     /* open room file */
  211.     if ((roomfl = fopen(temp, R_W_ANY)) == NULL) {
  212.     if ((roomfl = fopen(temp, W_R_ANY)) == NULL)
  213.         exit(printf("Hey! Can't create the room file?"));
  214.     }
  215.     initRoomBuf(&roomBuf);
  216.     strcpy(roomTab[0].rtname, cfg.codeBuf + cfg.bRoom);
  217.     strcpy(roomTab[1].rtname, "Mail");
  218.     strcpy(roomTab[2].rtname, "Aide");
  219.     for (c = 3; c < MAXROOMS; c++)
  220.     roomTab[c].rtname[0] = 0;    /* Convenient way to zap roomTab */
  221. }
  222.  
  223. /*
  224.  * main()
  225.  *
  226.  * This directs salvage proceedings and collects profits.
  227.  */
  228. int main(void);
  229. int main()
  230. {
  231.     printf("RECOVER2 - %s\n\n", COPYRIGHT);
  232.     init();
  233.     puts("No error msgs.  Go off and do something for an hour (or 2).\n");
  234.     XzapRoomFile();
  235.     doRest();
  236.     indexRooms();
  237.     writeSysTab();
  238.     printf("Don't need to reconfigure.\n");
  239.     return 0;
  240. }
  241.  
  242. /*
  243.  * noteRoom()
  244.  *
  245.  * This function will enter a room into RAM index array.
  246.  */
  247. void noteRoom()
  248. {
  249.     int   i;
  250.     MSG_NUMBER last;
  251.  
  252.     last = 0l;
  253.     for (i = 0;  i < MSGSPERRM;  i++)  {
  254.     if (roomBuf.msg[i].rbmsgNo > last) {
  255.         last = roomBuf.msg[i].rbmsgNo;
  256.     }
  257.     }
  258.     roomTab[thisRoom].rtlastMessage = last    ;
  259.     strCpy(roomTab[thisRoom].rtname, roomBuf.rbname) ;
  260.     roomTab[thisRoom].rtgen    = roomBuf.rbgen  ;
  261.     roomTab[thisRoom].rtflags.INUSE    = roomBuf.rbflags.INUSE;
  262.     roomTab[thisRoom].rtflags.PUBLIC   = roomBuf.rbflags.PUBLIC;
  263.     roomTab[thisRoom].rtflags.ISDIR    = roomBuf.rbflags.ISDIR;
  264.     roomTab[thisRoom].rtflags.PERMROOM = roomBuf.rbflags.PERMROOM;
  265.     roomTab[thisRoom].rtflags.SKIP    = roomBuf.rbflags.SKIP;
  266.     roomTab[thisRoom].rtFlIndex    = roomBuf.rbFlIndex;
  267. }
  268.  
  269. /*
  270.  * XzapRoomFile()
  271.  *
  272.  * This function erases and re-initializes ctdlroom.sys.
  273.  */
  274. void XzapRoomFile()
  275. {
  276.     int i;
  277.  
  278.     roomBuf.rbflags.PUBLIC   = TRUE;    /* This should be safe    */
  279.     roomBuf.rbflags.INUSE    = FALSE;
  280.     roomBuf.rbflags.ISDIR = FALSE;
  281.     roomBuf.rbflags.PERMROOM = FALSE;
  282.     roomBuf.rbgen    = 0;
  283.     roomBuf.rbname[0]   = 0;    /* unnecessary -- but I like it...    */
  284.     roomBuf.rbFlIndex   = 0;
  285.     for (i=0;  i<MSGSPERRM;  i++) {
  286.     roomBuf.msg[i].rbmsgNo = 0l;
  287.     roomBuf.msg[i].rbmsgLoc = 0;
  288.     }
  289.  
  290.     printf("maxrooms=%d\n", MAXROOMS);
  291.  
  292.     for (i=0;  i<MAXROOMS;  i++) {
  293.     RoomSys(i);
  294.     printf("clearing room %d\n", i);
  295.     putRoom(i);
  296.     }
  297.  
  298.     /* Lobby> always exists -- guarantees us a place to stand! */
  299.     thisRoom    = 0    ;
  300.     strcpy(roomBuf.rbname, cfg.codeBuf + cfg.bRoom)    ;
  301.     roomBuf.rbflags.PERMROOM = TRUE;
  302.     roomBuf.rbflags.PUBLIC   = TRUE;
  303.     roomBuf.rbflags.INUSE    = TRUE;
  304.     RoomSys(LOBBY);
  305.     putRoom(LOBBY);
  306.  
  307.     /* Mail> is also permanent...    */
  308.     thisRoom    = MAILROOM;
  309.     strcpy(roomBuf.rbname, "Mail");
  310.     RoomSys(MAILROOM);
  311.     putRoom(MAILROOM);
  312.  
  313.     /* Aide> also...    */
  314.     thisRoom    = AIDEROOM;
  315.     strcpy(roomBuf.rbname, "Aide");
  316.     roomBuf.rbflags.PUBLIC = FALSE;
  317.     RoomSys(AIDEROOM);
  318.     putRoom(AIDEROOM);
  319. }
  320.